import com.alibaba.druid.support.spring.stat.SpringStatUtils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * 功能描述：
 *
 * @Author: ygq
 * @Date: 2021/12/9 10:25
 */
public class MainTest {
    public static void main(String[] args) throws IOException {
        //1. 得到输入流对象
        InputStream inputStream = Resources.getResourceAsStream("MyBatis.xml");
        //2. 创建会话工厂建造类
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        //3. 通过建造类得到会话工厂类
        SqlSessionFactory factory = builder.build(inputStream);
        //4. 通过会话工厂得到会话对象
        SqlSession session = factory.openSession(true);
        //5. 会话对象得到UserMapper接口的代理对象
        UserMapper userMapper = session.getMapper(UserMapper.class);
        // 生成了代理对象：org.apache.ibatis.binding.MapperProxy@198b6731
        System.out.println(userMapper);
        //6. 执行查询操作
        List<User> users = userMapper.findALL();
        for (User user : users) {
            System.out.println(user);
        }
//        int addUser = userMapper.addUser(new User(5, "zhangsan", 13, "123com"));
//        if(addUser>0) {
//            System.out.println("添加成功");
//        }
//        else {
//            System.out.println("添加失败");
//        }
//        int i = userMapper.deleteUserById(2);
//        if (i>0) System.out.println("删除成功");
//        else {
//            System.out.println("删除失败");
//        }
//        int i1 = userMapper.updateUser(new User(3, "zhangzifeng", 21, "ilycom"));
//        if(i1>0) System.out.println("修改成功");
//        else System.out.println("修改失败");
//        默认是手动提交事务
//        session.commit();
//        session.update("UserMapper.updateUser",new User(6,"lisi",12,"email"));
//        7. 关闭会话
        session.close();

    }

}
